package cn.tedu.sims.service.impl;

import cn.tedu.sims.ex.ServiceException;
import cn.tedu.sims.mapper.LocationMapper;
import cn.tedu.sims.mapper.RoleMapper;
import cn.tedu.sims.mapper.UserManageMapper;
import cn.tedu.sims.mapper.UserMapper;
import cn.tedu.sims.pojo.dto.UserManageAddNewDTO;
import cn.tedu.sims.pojo.dto.UserManageDTO;
import cn.tedu.sims.pojo.dto.UserManageUpdateNewDTO;
import cn.tedu.sims.pojo.entity.InternalUnit;
import cn.tedu.sims.pojo.entity.User;
import cn.tedu.sims.pojo.entity.UserRole;
import cn.tedu.sims.pojo.vo.UserLoginInfoVO;
import cn.tedu.sims.pojo.vo.UserManageListItemVO;
import cn.tedu.sims.service.IUserManageService;
import cn.tedu.sims.web.ServiceCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
@Slf4j
public class UserManageServiceImpl implements IUserManageService {
    @Autowired
    private UserManageMapper userManageMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private LocationMapper locationMapper;
    @Autowired
    private RoleMapper roleMapper;

    @Override
    public List<UserManageListItemVO> list() {
        log.debug("开始处理[用户管理模块列表]的业务,无参数");
        return userManageMapper.list();
    }

    @Override
    public List<UserManageListItemVO> selectUserManage(UserManageDTO userManageDTO) {
        log.debug("开始处理[模糊查询用户管理列表]的业务,参数{}",userManageDTO);
        return userManageMapper.selectUserManage(userManageDTO);
    }


    @Override
    public void insert(UserManageAddNewDTO userManageAddNewDTO) {
        log.debug("开始处理[添加用户]的业务,参数{}",userManageAddNewDTO);
        //用户表
        /*
         * 检查添加用户的用户名是否与数据库的用户名重复
         */
        int count = userManageMapper.countByName(userManageAddNewDTO.getUsername());
        if(count>0){
            String message="添加用户失败，此用户名已经存在";
            throw new ServiceException(ServiceCode.ERR_CONFLICT,message);
        }

        count = userManageMapper.countByInternalUnitId(userManageAddNewDTO.getInternalUnitId());
        if(count!=1){
            String message="添加用户失败，此单位不存在";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }

        count = roleMapper.countById(userManageAddNewDTO.getRoleId());
        if (count!=1){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"角色不存在");
        }

        count = userMapper.countByPhone(userManageAddNewDTO.getPhone());
        if (count!=0){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"手机号重复");
        }

        count= locationMapper.countById(userManageAddNewDTO.getLocationId());
        if (count!=1){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"地区不存在");
        }

        User user=new User();
        user.setUsername(userManageAddNewDTO.getUsername());

        //密码加密
        String password = userManageAddNewDTO.getPassword();
        BCryptPasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
        String encodePassword = passwordEncoder.encode(password);
        user.setPassword(encodePassword);

        user.setReal_name(userManageAddNewDTO.getRealName());
        user.setPhone(userManageAddNewDTO.getPhone());
        user.setState(1);
        user.setLocationId(userManageAddNewDTO.getLocationId());
        user.setInternalUnitId(userManageAddNewDTO.getInternalUnitId());


        int rows = userManageMapper.insertUser(user);
        if (rows !=1){
            String message = "添加用户失败,服务器忙,请稍后再试! ";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        };
        //角色表
        UserRole userRole=new UserRole();
        userRole.setUserId(user.getId());
        userRole.setRoleId(userManageAddNewDTO.getRoleId());
        rows = userManageMapper.insertUserRole(userRole);
        if (rows !=1){
            String message = "添加用户失败,服务器忙,请稍后再试! ";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }



    }

    @Override
    public void deleteByUserId(Long id) {
        log.debug("开始处理[删除用户]的业务,参数{}",id);
        int count = userManageMapper.countByUserId(id);
        if(count!=1){
            String message="删除用户失败，此用户不存在";
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,message);
        }
        /*count= userManageMapper.countByRoleId(id);
        if(count > 0){
            String message="删除用户失败，存在关联异常";
            throw new ServiceException(ServiceCode.ERR_DELETE,message);
        }*/

        /*int rows = userManageMapper.InternalUnitId(id);
        if (rows !=0){
            String message = "删除用户失败,存在单位关联异常";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_DELETE, message);
        }*/


         int rows = userManageMapper.deleteUserById(id);
        if (rows !=1){
            String message = "删除用户失败,服务器忙,请稍后再试! ";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
        rows = userManageMapper.deleteUserRoleById(id);
        if (rows != 1){
            String message = "删除用户失败,服务器忙,请稍后再试! ";
            log.debug(message);
            throw new ServiceException(ServiceCode.ERR_INSERT, message);
        }
    }


    @Override
    public void update(UserManageUpdateNewDTO userManageUpdateNewDTO) {
        int count = userMapper.countByPhone(userManageUpdateNewDTO.getPhone());
        if (count!=0){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"手机号重复");
        }

         count = roleMapper.countById(userManageUpdateNewDTO.getRoleId());
        if (count!=1){
            throw new ServiceException(ServiceCode.ERR_NOT_FOUND,"角色不存在");
        }

        User user=new User();
        user.setId(userManageUpdateNewDTO.getId());
        user.setReal_name(userManageUpdateNewDTO.getRealName());
        user.setPhone(userManageUpdateNewDTO.getPhone());
        user.setInternalUnitId(userManageUpdateNewDTO.getInternalUnitId());
        int row = userManageMapper.updateUser(user);
        if (row!=1){
            throw new ServiceException(ServiceCode.ERR_UPDATE,"修改失败,服务器繁忙");
        }


        UserRole userRole =new UserRole();
        userRole.setUserId(user.getId());
        userRole.setRoleId(userManageUpdateNewDTO.getRoleId());
        row = userManageMapper.updateUserRole(userRole);
        if (row!=1){
            throw new ServiceException(ServiceCode.ERR_UPDATE,"修改失败,服务器繁忙");
        }
    }


}
